'''
Created on 2010-4-18

@author: Colin Han
'''
import hashlib
import wsgiref.handlers

from google.appengine.ext.webapp import template, \
    WSGIApplication, RequestHandler
from google.appengine.api import users
    
from model import localUser
    
class userRegister(RequestHandler):
    def get(self):
        user = localUser.gql('WHERE sysUser = :1', users.get_current_user()).get()
        if user:
            self.redirect('/')
            return
        
        dict = {}
        html = template.render('''themes/default/templates/userRegister.html''', dict, False)
        self.response.out.write(html)
        
    def post(self):
        userId = self.request.get('userName')
        m = hashlib.md5()
        m.update(self.request.get('password'))
        pwd = m.hexdigest()
        
        user = localUser(userId = userId,
                         passwordToken = pwd,
                         sysUser = users.get_current_user())
        user.save();
        
        self.redirect('/')

def main():
    #template.register_template_library('filter')
    application = WSGIApplication(
                    [('/register.html', userRegister),
                     ],debug=True)
    wsgiref.handlers.CGIHandler().run(application)
    
if __name__ == '__main__':
    main()